package com.itheima.demo;

import java.util.Arrays;

public class Demo005 {
    public static void main(String[] args) {
        int[] arr = {11, 22, 445, 2, 6, 8, 0, 5, 21, 44};
        //使用冒泡排序;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
        //使用二分查找数值对应的索引值;
        int number = 5;
        int index = getIndex(arr, number);
        System.out.println(index);
    }
    //二分查找;
    private static int getIndex(int[] arr, int number) {
        int min = 0;
        int max = arr.length - 1;
        while (min <= max) {
            int mid = min + (max - min) / 2;
            if (arr[mid] > number) {
                max = mid - 1;
            } else if (arr[mid] < number) {
                min = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}
